<?php

namespace App\Http\Controllers\Dashboard;


use App\Models\Org;
use App\Models\User;
use App\Models\WebsiteCity;
use App\Models\WebsiteLoanClass;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;


class SearchController extends BaseController
{
    /**
     * 搜索当前机构及下级机构
     * @param Request $request
     * @return JsonResponse
     */
    public function org(Request $request) {
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => Org::where('name', "like", sprintf("%%%s%%",  $request->k))
                ->ofOrgCode($request->user())->limit(10)->select(["id","name","org_code"])->get()
        ]);
    }


    public function orgCity(Request $request) {

        $data =  WebsiteCity::where('name', "like", sprintf("%%%s%%",  $request->k))
            ->ofOrgCode($request->user())->limit(10)->select(["id","name","org_code"])->get();
        foreach ($data as $value){
            $value->name = $value->name.'-'.$value->org_code;
        }
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => $data
        ]);
    }

    public function loanClass(Request $request) {
        $data =  WebsiteLoanClass::where('name', "like", sprintf("%%%s%%",  $request->k))
            ->ofOrgCode($request->user())->limit(10)->select(["id","name"])->get();
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => $data
        ]);
    }






    /**
     * 搜索当前机构及下级机构的用户
     * @return JsonResponse
     */
    public function user(Request $request) {
        $data = User::where('name', "like", sprintf("%%%s%%",  $request->k))
            ->Orwhere('mobile', "like", "%$request->k%")
            ->ofOrgCode($request->user())->limit(10)->select(["id","name","mobile"])->get()
            ->makeHidden( ['status_name', 'gender_name', 'kyc_name']);

        foreach ($data as $value){
            $value->name = $value->name.'-'.$value->mobile;
        }


        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => $data
        ]);
    }
}
